#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>

void* func(void*arg){

    //int distance=(int)arg;
    int i;
    for(i=1;i<=50;i++){
        std::cout<<"id:"<<pthread_self()<<" num:"<<i<<std::endl;
        printf("\n");
        int time=(int)(drand48()*10000);
        usleep(time);
    }


    return (void*)9;
}

int main(){

    int err;
    pthread_t rabbit,turtle;

    if((err=pthread_create(&rabbit,NULL,func,NULL)!=0)){

        perror("pthread_create");

    }

    if((err=pthread_create(&turtle,NULL,func,NULL))!=0){

        perror("pthread_create");

    }


    //主控线程调用pthread_join()自己会阻塞//知道线程执行结束才会执行主控线程本身
    
    pthread_join(rabbit,NULL);
    pthread_join(rabbit,NULL );

    sleep(10);
    std::cout<<"main thread"<<pthread_self()<<std::endl;





    return 0;
}